/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wii.elpro.web.action;

import com.wii.elpro.dao.DepartemenDAOInterface;
import com.wii.elpro.dao.hb.DepartemenDAOImpl;
import com.wii.elpro.entity.Departemen;
import com.wii.elpro.entity.User;
import com.wii.elpro.web.form.DepartemenNewForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author hp02
 */
public class DepartemenSaveAction extends Action {

    public DepartemenSaveAction() {
    }
    
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession sess = request.getSession(true);
        User user = (User)sess.getAttribute("userSession");       
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }  
        String forward;
        DepartemenNewForm deptForm = (DepartemenNewForm) form;
        Departemen dept = new Departemen();
        DepartemenDAOInterface dao = new DepartemenDAOImpl();
        dept.setKeterangan(deptForm.getKeterangan());
        dept.setKode(deptForm.getKode());
        dept.setNama(deptForm.getNama());
        ActionMessages messages = new ActionMessages();       
        String fw = request.getParameter("submit");
        try {
            dao.save(dept);
            messages.add("customMessage", new ActionMessage("customMessage", "Your data is successfully saved."));
            forward = fw;
        } catch (Exception e) {
            messages.add("message", new ActionMessage("customMessage", "Application is fail to save your data. Please contact my developer."));
            e.printStackTrace();
            forward = "fail";
        }
        saveMessages(sess, messages);
        return mapping.findForward(forward);
    }
}
